using Senparc.Weixin.Exceptions;
using Senparc.Weixin.MessageHandlers;
using System;
using System.Web.Mvc;
namespace Senparc.Weixin.MP.MvcExtension
{
	public class WeixinResult : ContentResult
	{
		protected IMessageHandlerDocument _messageHandlerDocument;
		public string Content
		{
			get
			{
				string result;
				if (base.Content != null)
				{
					result = base.Content;
				}
				else
				{
					if (this._messageHandlerDocument != null && this._messageHandlerDocument.FinalResponseDocument != null)
					{
						result = this._messageHandlerDocument.FinalResponseDocument.ToString();
					}
					else
					{
						result = null;
					}
				}
				return result;
			}
			set
			{
				base.Content= value;
			}
		}
		public WeixinResult(string content)
		{
			base.Content=content;
		}
		public WeixinResult(IMessageHandlerDocument messageHandlerDocument)
		{
			this._messageHandlerDocument = messageHandlerDocument;
		}
		public override void ExecuteResult(ControllerContext context)
		{
			if (base.Content == null)
			{
				if (this._messageHandlerDocument == null)
				{
					throw new WeixinException("执行WeixinResult时提供的MessageHandler不能为Null！", null);
				}
				if (this._messageHandlerDocument.FinalResponseDocument != null)
				{
					context.HttpContext.Response.ClearContent();
					context.HttpContext.Response.ContentType = "text/xml";
					this._messageHandlerDocument.FinalResponseDocument.Save(context.HttpContext.Response.OutputStream);
				}
			}
			base.ExecuteResult(context);
		}
	}
}
